package com.atguigu.yygh.sta.controller;

import com.atguigu.yygh.hosp.result.R;
import com.atguigu.yygh.order.client.OrderFeignClient;
import com.atguigu.yygh.vo.order.OrderCountQueryVo;
import io.swagger.models.auth.In;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/sta")
public class StaController {
    @Autowired
    OrderFeignClient orderFeignClient;

    @GetMapping("/getOrderCount")
    public R getOrderCount(OrderCountQueryVo orderCountQueryVo){
        Map map = orderFeignClient.getOrderCount(orderCountQueryVo);
        List<String> dateList =(List<String>)map.get("dateList");

        //6个数量,就是存在的挂号日期
        List<Integer> countList =(List<Integer>)map.get("countList");

//        //两个集合将来需要调用订单服务获取
//        List<String> dateList = Arrays.asList("2023-08-10", "2023-08-19", "2023-08-20");
//        List<Integer> countList = Arrays.asList(20,10, 15);

        //不连续的日期,赋予默认值0
       // String dateBegin = dateList.get(0);  //开始的日期
        // String dateEnd = dateList.get(dateList.size() - 1);  //结束的日期
        String dateBegin = orderCountQueryVo.getReserveDateBegin();
        if(StringUtils.isEmpty(dateBegin)){
            dateBegin=dateList.get(0);
        }
        String dateEnd = orderCountQueryVo.getReserveDateEnd();
        if(StringUtils.isEmpty(dateEnd)){
            dateEnd=dateList.get(dateList.size() - 1);
        }

        //连续的日期范围
        List<String> rangeDateList=new ArrayList<>();

        //开始时间
        DateTime begin = new DateTime(dateBegin);
        while (true){
            rangeDateList.add(begin.toString("yyyy-MM-dd"));
            if(begin.toString("yyyy-MM-dd").equalsIgnoreCase(dateEnd)){
                break;
            }
            begin=begin.plusDays(1);
        }

        List<Integer> rangeCountList=new ArrayList<>();

        for (int i = 0; i < rangeDateList.size(); i++) {
            String date = rangeDateList.get(i);
            int index = dateList.indexOf(date);

            Integer count = 0;
            if(index>=0){
                count = countList.get(index);
            }
            rangeCountList.add(count);
        }
        return R.ok().data("dateList",rangeDateList).data("countList",rangeCountList);
    }
}
